<?php
define('LINK_ADMINCP_GALLERY', '/admincp/gallery');
class GalleryController extends Controller
{
	public $layout = 'main';

	public function actionIndex()
	{
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		$intTaxonomyId =  3;
		
		//query posts
		$args = array(
			'taxonomy_id' => $intTaxonomyId,
			'keyword' =>  '%'.Yii::app()->request->getParam('q', null).'%',
			'category_id' => Yii::app()->request->getParam('cat', null),
			'post_status' => Yii::app()->request->getParam('status', null),
			/*'created_date' => array(
				'from' => Yii::app()->request->getParam('dfrom', null),
				'to' => Yii::app()->request->getParam('dto', null)
			),*/
			'page' => $intPage,
			'limit' => $intRowsPerPage
		);
		$arrPosts = Posts::getInstance()->getList($args);
		$intTotalRows = isset($arrPosts[0]['total_rows'])?$arrPosts[0]['total_rows']:0;
		$args['keyword'] = trim($args['keyword'], '%');
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$arrCategories = Categories::getInstance()->getByParent();
		$this->render('index', array(
			'arrPosts' => $arrPosts,
			'objPages' => $objPages,
			'args' => $args,
			'arrPostStatus' => Posts::getInstance()->getListStatus(),
		));
		$this->setUrl();
	}
	
	public function actionForm()
	{
		$intPostId = Yii::app()->request->getParam('id', null);
		$intTaxonomyId =  3;
		//update post
		if(Yii::app()->request->isPostRequest)
		{
			//get param
			$arrParams = Yii::app()->request->getParam('post', null); 
			$arrParams['taxonomy_id'] = $intTaxonomyId;
			$refresh = false;

			//insert/update
			if(empty($intPostId)){
				$arrParams['post_creator_id'] = Yii::app()->admin->id;
				$intResult = Posts::getInstance()->insert($arrParams);
				$intPostId = $intResult;
				$refresh = true;
			}
			else{
				$arrParams['post_last_modifier_id'] = Yii::app()->admin->id;
				$intResult = Posts::getInstance()->update($intPostId, $arrParams);
			}

			//done
			Yii::app()->admin->setFlash('ok', Yii::t('admincp', 'Updated successfully!'));
			if($refresh){
				$this->refresh();
			} else{
				header('Location:'.$this->getUrl());
				exit;
			}
		}
		
		//load form
		$arrItemInfo = array();
		if(!empty($intPostId)){
			$arrItemInfo = Posts::getInstance()->get($intPostId);
		}

		//render layout
		$this->render('form', array(
			'arrItemInfo' => $arrItemInfo
		));
	}
	
	public function actionPhoto(){
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		$intPostParentId = Yii::app()->request->getParam('id', null);
		$intTaxonomyId =  3;
		
		//update photo
		if(Yii::app()->request->isPostRequest)
		{
			//add photo
			$arrNewPhotos = Yii::app()->request->getParam('photo', null);
			foreach($arrNewPhotos as $photo){
				$photo = trim($photo);
				if(!empty($photo)){
					$args = array(
						'taxonomy_id' => $intTaxonomyId,
						'post_parent' => $intPostParentId,					
						'post_title' => $photo,						
						'post_image' => $photo,
						'post_allow_search' => 1,
						'post_allow_comment' => 1,
						'post_status' => 1
					);
					Posts::getInstance()->insert($args);
				}
			}
			//edit photo
			$arrPhotos = array(
				'title' => Yii::app()->request->getParam('title', null),
				'intro' => Yii::app()->request->getParam('intro', null),
				'img' => Yii::app()->request->getParam('img', null)
			);
			if(!empty($arrPhotos['title'])){
				foreach($arrPhotos['title'] as $key=>$title){
					$id = $key;
					$args = array(
						'taxonomy_id' => $intTaxonomyId,
						'post_parent' => $intPostParentId,					
						'post_title' => $title,
						'post_intro' => $arrPhotos['intro'][$id],
						'post_image' => $arrPhotos['img'][$id],
						'post_allow_search' => 1,
						'post_allow_comment' => 1,
						'post_status' => 1
					);
					Posts::getInstance()->update($id, $args);
				}
			}
			$this->refresh();
		}
		
		$args = array(
			'taxonomy_id' => $intTaxonomyId,
			'post_parent' => $intPostParentId,
			'post_status' => 1,
			'page' => $intPage,
			'limit' => $intRowsPerPage
		);
		$arrPosts = Posts::getInstance()->getList($args);
		
		//render layout
		$this->render('photo', array(
			'intPostParentId' => $intPostParentId,
			'arrPosts' => $arrPosts
		));
		
		$this->setUrl();
	}
	
	public function actionDelete(){
		$intPostId = Yii::app()->request->getParam('id', null);
		if(!empty($intPostId))
			$result = Posts::getInstance()->delete($intPostId);
		header('Location:'.$this->getUrl());
		exit;
	}

}